<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Config;
use think\Db;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Shopboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        $seventtime = \fast\Date::unixtime('day', -7);
        $paylist = $createlist = [];
        for ($i = 0; $i < 7; $i++)
        {
            $day = date("Y-m-d", $seventtime + ($i * 86400));
            $createlist[$day] = mt_rand(20, 200);
            $paylist[$day] = mt_rand(1, mt_rand(1, $createlist[$day]));
        }
        $hooks = config('addons.hooks');
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
        $addonComposerCfg = ROOT_PATH . '/vendor/karsonzhang/fastadmin-addons/composer.json';
        Config::parse($addonComposerCfg, "json", "composer");
        $config = Config::get("composer");
        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');

        //$total_user_count = Db::name("user")->where("uid","not null")->count();//总会员数

        //$total_user_count_total = sprintf("%.2f",floatval(Db::name("finance")->where("source","paike")->where("fina_type","in")->field("sum(fina_cash) as fina_cash_total_user")->find()["fina_cash_total_user"]));//会员获利总数

        //$shop_admin_count = Db::name("admin")->where("platform","2")->count();//总商户数
        $shop_admin_id = 0;
        if(session("admin")["athur_id"]==0) {
            $shop_admin_id = session('admin')['id'];
        }elseif(session("admin")["athur_id"]==1){
            $shop_admin_id = session("admin")["athor"];
        }
        //$shop_admin_id = session('admin')['id'];

        $shop_admin_money_total =sprintf("%.2f",floatval(Db::name("finance")->where("shop_admin_id",$shop_admin_id)->where("source","shop")->where("fina_type","in")->where("obj_type","recharge")->field("sum(fina_cash) as fina_cash_total_shop")->find()["fina_cash_total_shop"]));//商户充值总数



        //echo $shop_admin_id."aa";exit;

        $join[] = ["sd_shop p","p.shop_id = a.shop_id","left"];

        $order_total_count = Db::name("shop_order")->alias("a")->join($join)->where("a.is_cancel",1)->where("p.shop_admin_id",$shop_admin_id)->count();//总订单数

//        //echo "aa";exit;
        $task_total_count = Db::name("shop_task")->where("task_status",2)->where("shop_admin_id",$shop_admin_id)->count();//任务订单数


        $task_total_count_goal = sprintf("%.2f",floatval(Db::name("shop_order")->where("shop_admin_id",$shop_admin_id)->where("status",13)->where("finished_time","not null")->field("sum(order_goldbean_amount) as order_goldbean_amount_total")->find()["order_goldbean_amount_total"]));

        //$finance_total_count_in = sprintf("%.2f",floatval(Db::name("finance")->where("source","system")->where("fina_type","in")->field("sum(fina_cash) as fina_cash_total")->find()["fina_cash_total"]));//系统获取的利润


        //$finance_total_count_out = sprintf("%.2f",floatval(Db::name("finance")->where("source","system")->where("fina_type","out")->field("sum(fina_cash) as fina_cash_total")->find()["fina_cash_total"]));//系统总支出

        //$finance_total = sprintf("%.2f",floatval($finance_total_count_in["fina_cash_total"])-floatval($finance_total_count_out["fina_cash_total"]));


        $dongjie_zijin = Db::name("shop_admin_wallet")->where("wallet_id","not null")->where("admin_id",$shop_admin_id)->value("frozen_money");

        $dongjie_zijin_total =  sprintf("%.2f",floatval($dongjie_zijin));



        $this->view->assign([
            //'total_user_count'        => $total_user_count,
            //'total_user_count_total'  => $total_user_count_total,
            //'shop_admin_count'       => $shop_admin_count,
            'shop_admin_money_total'=>$shop_admin_money_total,
            'order_total_count'       => $order_total_count,
            'task_total_count' => $task_total_count,
            //'finance_total'   => $finance_total,
            'dongjie_zijin_total'  => $dongjie_zijin_total,
            'task_total_count_goal'=>$task_total_count_goal

            //'finance_total_count_in'=>$finance_total_count_in,
            //'finance_total_count_out'=>$finance_total_count_out
//            'todayorder'       => 2324,
//            'unsettleorder'    => 132,
//            'sevendnu'         => '80%',
//            'sevendau'         => '32%',
//            'paylist'          => $paylist,
//            'createlist'       => $createlist,
//            'addonversion'       => $addonVersion,
//            'uploadmode'       => $uploadmode
        ]);

        return $this->view->fetch();
    }

}
